//==========系统公告

package com.hnkywl.sts.dao.base;

import com.hnkywl.sts.entity.base.Xtgg;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.Pager;
import com.hnkywl.sts.util.QuerySearcher;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-29
 * Time: 下午9:54
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class XtggDao extends SimpleHibernateTemplate<Xtgg, Long> {
    /**
     * 查询全部系统公告
     *
     * @param ggbt
     * @param ggnr
     * @param ggzt
     * @param xtggcjStart
     * @param xtggcjEnd
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Xtgg> findXtggList(String ggbt, String ggnr, String ggzt, Date xtggcjStart, Date xtggcjEnd, int pageNo, int pageSize) {
        Page<Xtgg> page = new Page<Xtgg>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("dateCreated");
        /*Criteria criteria = getSession().createCriteria(this.entityClass);*/
        /*.createAlias("gsjgList")
        .setProjection(Projections.projectionList()
                .add(Property.forName("id").as("id"))
                .add(Property.forName("gsjgList").as("gsjgList"))
                .add(Projections.sqlProjection("ggbt as ggbt", new String[]{"ggbt"}, new Type[]{Hibernate.STRING}))
                .add(Projections.sqlProjection("ggnr as ggnr", new String[]{"ggnr"}, new Type[]{Hibernate.STRING}))
                .add(Projections.sqlProjection("ggzt as ggzt", new String[]{"ggzt"}, new Type[]{Hibernate.STRING}))
                .add(Property.forName("fbdate").as("fbdate"))
                .add(Property.forName("dateCreated").as("date_created"))
                .add(Property.forName("lastUpdated"))
        )
        .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);*/

        List<Criterion> criterionList = new ArrayList<Criterion>();
        if (StringUtils.isNotEmpty(ggbt)) {
            criterionList.add(Property.forName("ggbt").like(ggbt, MatchMode.START));
        }
        if (StringUtils.isNotEmpty(ggnr)) {
            criterionList.add(Property.forName("ggnr").like(ggnr, MatchMode.START));
        }
        if (StringUtils.isNotEmpty(ggzt)) {
            criterionList.add(Restrictions.eq("ggzt", ggzt));
        }

        if (xtggcjStart != null) {
            criterionList.add(Restrictions.gt("dateCreated", xtggcjStart));
        }
        if (xtggcjEnd != null) {
            criterionList.add(Restrictions.lt("dateCreated", xtggcjEnd));
        }

        /*RegionDao.createDataRegion(criteria);*/
        // RegionDao.createDataRegion(criteria);
        Page list = this.findByCriteria(page, criterionList);
        return list;
    }

    public Page<Xtgg> findAll(Pager pager, QuerySearcher queryXtgg) {
        String where = queryXtgg == null ? "" : queryXtgg.getHql();
        return this.find(pager.getPage(), "from Xtgg " + where + " order by zd desc, dateCreated desc");
    }

    public List<Xtgg> findPub() {
        Page page = new Page();
        page.setPageNo(1);
        page.setPageSize(7);
        page=this.find(page,"select xt from Xtgg xt,Gsjg gs where xt.ggzt = '1' and (gs in elements(xt.gsjgList) and gs.id=?) order by xt.zd desc,xt.fbdate desc", YgService.getCurrentYgGsId());
        return page.getResult();
    }
}